package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.tmodel.NursingPlanDetail;

@JsonApiResourceRepository(NursingPlanDetail.class)
@Component
public class NursingPlanDetailRepository extends BaseRepository<NursingPlanDetail> {
	private final String TYPE_OF_REHABILITATION = "serviceType4";// 服务类型:康复计划
	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(NursingPlanDetail.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<NursingPlanDetail> findAll(final QueryParams requestParams) {
		String action = ActionContextHolder.getContext().getAction();
		List<NursingPlanDetail> itemList = new ArrayList<NursingPlanDetail>();
		Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
		long uid = -1;
		if ("spec_countService".equals(action)) {// 计次服务
			if (map.containsKey("uid") && map.get("uid") != null && !"".equals(map.get("uid"))) {
				uid = Long.parseLong(map.get("uid"));
				return nursingService.getCountServiceItem(uid);
			}
			return this.filterRehabilitation(map, itemList);
		} else if ("customer_plan".equals(action)) {// 查询老人 某周护理计划
			if (map.containsKey("cid") && map.get("cid") != null && !"".equals(map.get("cid")) && // customerid
					map.containsKey("yearTab") && map.get("yearTab") != null && !"".equals(map.get("yearTab")) && // 年份
					map.containsKey("weekIndex") && map.get("weekIndex") != null && !"".equals(map.get("weekIndex")) // 第几周
			) {
				long customerId = Long.parseLong(map.get("cid"));
				Integer yearTab = Integer.parseInt(map.get("yearTab"));
				Integer weekIndex = Integer.parseInt(map.get("weekIndex"));
				return this.filterRehabilitation(map, (List<NursingPlanDetail>) nursingService
						.getCustomerNursePlanNum(customerId, yearTab, weekIndex));
			}
			return this.filterRehabilitation(map, itemList);
		} else if ("jujia".equals(action)) {
			return super.findAll(requestParams);
		} else {
			return this.filterRehabilitation(map, (List<NursingPlanDetail>) super.findAll(requestParams));
		}
	}

	/**
	 * 过滤康复计划
	 *
	 * @author ytche
	 * @param map
	 * @param itemList
	 * @return
	 * @date 2017年8月7日 下午6:21:58
	 */
	private List<NursingPlanDetail> filterRehabilitation(Map<String, String> map, List<NursingPlanDetail> itemList) {

		if (itemList == null || itemList.size() < 1) {
			return itemList;
		}
		// 判断是否查询康复计划
		Boolean isForRehabilitation = map.containsKey("item.item.serviceType.typeCode")
				&& map.get("item.item.serviceType.typeCode").equals("TYPE_OF_REHABILITATION");
		// 处理计划服务中的康复计划和非康复计划,并根据查询条件返回结果
		try {
			Predicate<NursingPlanDetail> predicate = isForRehabilitation
					? p -> p.getItem() != null
							&& p.getItem().getItem().getServiceType().getTypecode().equals(this.TYPE_OF_REHABILITATION)
					: p -> p.getItem() == null || !p.getItem().getItem().getServiceType().getTypecode()
							.equals(this.TYPE_OF_REHABILITATION);
			return itemList.stream().filter(predicate).collect(Collectors.toList());
		} catch (NullPointerException e) {
			throw e;
		}
	}

	@Override
	@JsonApiSave
	public NursingPlanDetail save(NursingPlanDetail data) {
		String action = ActionContextHolder.getContext().getAction();
		if (action == null || "".equals(action)) {
			data = super.save(data);
		} else if ("jujia".equals(action)) {
			if (data.getId() == null) {
				// 下单
				data = nursingService.createJujiaPlanDetail(data);
			} else {
                //分配订单

				//
				//取消订单
				//data = nursingService.cancelJujiaPlanDetail(data);
				data = super.save(data);
			}
		}
		return data;
	}

}
